/**
 * QuestionController.java
 * Version 1.0.0
 * Created on 2018年7月16日
 *
 */
package com.yincheng.oes.business.base.controller;

import com.yincheng.oes.business.base.entity.Question;
import com.yincheng.oes.business.base.entity.QuestionForm;
import com.yincheng.oes.business.base.service.QuestionService;
import com.yincheng.oes.business.util.dictionary.DictionaryService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import static com.yincheng.oes.business.util.PaperUtils.getAnswersList;

/**
 * @author yangsy
 * @version 1.0.0
 * @since
 * 创建时间：2018年7月16日
 * 功能描述：题目信息数据获取修改控制层
 * 邮箱地址：446948434@qq.com
 */
@Controller
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 根据题目id获取题目信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/getQuestionById.json", method = RequestMethod.GET)
    @ResponseBody
    public Question getQuestionById(Integer id) {
        return questionService.getQuestionById(id);
    }

    /**
     * 创建题目
     *
     * @param view
     * @return
     */
    @RequestMapping(value = "/draft", method = RequestMethod.GET)
    public ModelAndView draft(ModelAndView view) {

        Question question = questionService.createQuestion();

        view.addObject("id", question.getId());
        view.setViewName("redirect:/question/editPage.html");
        return view;
    }

    @RequestMapping(value = "/draftAsk", method = RequestMethod.GET)
    public ModelAndView draftAsk(ModelAndView view) {

        Question question = questionService.createQuestion();

        view.addObject("id", question.getId());
        view.setViewName("redirect:/question/draftAskPage.html");
        return view;
    }

    @RequestMapping(value = "/editJump", method = RequestMethod.GET)
    public ModelAndView editJump(ModelAndView view, Integer id) {
        Question question = questionService.getQuestionById(id);
        view.addObject("id", question.getId());
        String quesSource = question.getQuesSource();
        if ("2".equals(quesSource)) {
            view.setViewName("redirect:/question/editAskPage.html");
        } else {
            view.setViewName("redirect:/question/editPage.html");
        }
        return view;
    }

    @RequestMapping(value = "/readJump", method = RequestMethod.GET)
    public ModelAndView readJump(ModelAndView view, Integer id) {
        Question question = questionService.getQuestionById(id);
        view.addObject("id", question.getId());
        String quesSource = question.getQuesSource();
        if ("2".equals(quesSource)) {
            view.setViewName("redirect:/question/readAskPage.html");
        } else {
            view.setViewName("redirect:/question/readPage.html");
        }
        return view;
    }

    /**
     * 根据题目id得到相关联的题目列表
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/getRelatedQuestionById.json", method = RequestMethod.GET)
    @ResponseBody
    public List<Question> getRelatedQuestionById(Integer id) {
        return questionService.getRelatedQuestionById(id);
    }

    /**
     * 题目信息创建页面
     *
     * @param view
     * @return
     */
    @RequestMapping(value = "/draftPage.html", method = RequestMethod.GET)
    @Deprecated
    public ModelAndView draftPage(ModelAndView view) {
        QuestionForm form = new QuestionForm();
        view.addObject("form", form);

        setView(view);
        view.setViewName("/business/question/question-draft");
        return view;
    }

    /**
     * @param view
     */
    private void setView(ModelAndView view) {
        view.addObject("quesTypes", dictionaryService.getByModel("ques_type"));
        List<String> answers = getAnswersList();
        view.addObject("answers", answers);
    }

    /**
     * 创建题目信息
     *
     * @param form
     */
    @RequestMapping(value = "/createQuestion.json", method = RequestMethod.POST)
    @Deprecated
    public void createQuestion(QuestionForm form) {
        questionService.createQuestion(form);
    }

    /**
     * 题目信息编辑页面
     *
     * @param view
     * @param id
     * @return
     */
    @RequestMapping(value = "/editPage.html", method = RequestMethod.GET)
    public ModelAndView editPage(ModelAndView view, Integer id) {
        setView(view);

        QuestionForm form = questionService.getQuestionFormById(id);
        view.addObject("form", form);
        view.setViewName("/business/question/question-edit");
        return view;
    }

    @RequestMapping(value = "/editAskPage.html", method = RequestMethod.GET)
    public ModelAndView editAskPage(ModelAndView view, Integer id) {
        setView(view);

        QuestionForm form = questionService.getQuestionFormById(id);
        view.addObject("form", form);
        view.setViewName("/business/question/question-ask-edit");
        return view;
    }

    @RequestMapping(value = "/draftAskPage.html", method = RequestMethod.GET)
    public ModelAndView draftAskPage(ModelAndView view, Integer id) {
        setView(view);

        QuestionForm form = questionService.getQuestionFormById(id);
        view.addObject("form", form);
        view.setViewName("/business/question/question-ask-draft");
        return view;
    }

    /**
     * 保存题目信息
     *
     * @param form
     */
    @RequestMapping(value = "/saveQuestion.json", method = RequestMethod.POST)
    public void saveQuestion(QuestionForm form) {
        questionService.saveQuestion(form);
    }

    /**
     * 题目信息只读页面
     *
     * @param view
     * @param id
     * @return
     */
    @RequestMapping(value = "/readPage.html", method = RequestMethod.GET)
    public ModelAndView readPage(ModelAndView view, Integer id) {
        setView(view);

        QuestionForm form = questionService.getQuestionFormById(id);
        view.addObject("form", form);
        view.setViewName("/business/question/question-read");
        return view;
    }

    @RequestMapping(value = "/readAskPage.html", method = RequestMethod.GET)
    public ModelAndView readAskPage(ModelAndView view, Integer id) {
        setView(view);

        QuestionForm form = questionService.getQuestionFormById(id);
        view.addObject("form", form);
        view.setViewName("/business/question/question-ask-read");
        return view;
    }
}
